package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;
import com.qiantu.phone.widget.SectionSeekBar;
import com.qiantu.phone.widget.StatusSeekBar;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class BrightnessSettingActivityNew extends AppActivity {
    private SettingBar settingAction;
    private TextView tvCondition;
    private RadioGroup whenRadioGroup;
    private StatusSeekBar statusSeekBar;
    private SectionSeekBar sectionSeekBar;;
    private String logicTypeStr;
    private String unit = "%";
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageDeviceCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_brightness_setting_new;
    }

    @Override
    protected void initView() {
        mLinkageCondition =  getSerializable("condition");
        settingAction = findViewById(R.id.setting_action);
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        statusSeekBar = findViewById(R.id.status_seek_bar);
        sectionSeekBar = findViewById(R.id.section_seek_bar);
        setOnClickListener(settingAction);
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_less_than:
                        mLinkageCondition.setCompareTypeId(1);
                        logicTypeStr = getString(R.string.less_than);
                        break;
                    case R.id.type_equal_to:
                        mLinkageCondition.setCompareTypeId(2);
                        logicTypeStr = getString(R.string.equal_to);
                        break;
                    case R.id.type_greater_than:
                        mLinkageCondition.setCompareTypeId(3);
                        logicTypeStr = getString(R.string.greater_than);
                        break;
                }
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mLinkageCondition.getBeginValue() + unit));
            }
        });
//        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
        statusSeekBar.setOnSeekBarChangeListener(new StatusSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int value) {
                mLinkageCondition.setBeginValue(value);
                mLinkageCondition.setEndValue(value);
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mLinkageCondition.getBeginValue() + unit));
            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int value) {
                mLinkageCondition.setBeginValue(value);
                mLinkageCondition.setEndValue(value);
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mLinkageCondition.getBeginValue() + unit));
            }
        });
        sectionSeekBar.setOnSeekBarChangeListener(new SectionSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int beginValue, int endValue) {
                if (mLinkageCondition.getBeginValue() == beginValue && mLinkageCondition.getEndValue() == endValue) {
                    return;
                }
                mLinkageCondition.setBeginValue(beginValue);
                mLinkageCondition.setEndValue(endValue);
            }

            @Override
            public void onStartTrackingTouch() {
            }

            @Override
            public void onStopTrackingTouch(int beginValue, int endValue) {
            }
        });
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();
        String stateName = getString("type");
        //当前联动空，才去
        if (mLinkageCondition == null) {
            mLinkageCondition = new LinkageNewBean.LinkageDeviceCondition();
            mLinkageCondition.setConditionTypeId(8);
            mLinkageCondition.setCompareTypeId(1);
            mLinkageCondition.setAreaTypeId(1);
            mLinkageCondition.setDeviceSerialNo(getString("device_serial_no"));
            mLinkageCondition.setAreaName(getString("floor_room_name"));
            mLinkageCondition.setAreaCode(getString("room_serial_no"));
            mLinkageCondition.setActionTypeId(1);
            mLinkageCondition.setOperatorTypeId(1);
            if(stateName.equals(LLDeviceUtil.LIGHT)){
                mLinkageCondition.setBeginValue(50);
                mLinkageCondition.setEndValue(50);
                unit = "%";
            }else if(stateName.equals(LLDeviceUtil.KELVIN)){
                mLinkageCondition.setBeginValue(3000);
                mLinkageCondition.setEndValue(3000);
                unit = "K";
            }else if(stateName.equals(LLDeviceUtil.STROKE_PERCENTAGE)){
                mLinkageCondition.setBeginValue(50);
                mLinkageCondition.setEndValue(50);
                unit = "%";
            }
            mLinkageCondition.setIcon(getString("device_image"));
            mLinkageCondition.setDescription(getString("device_name"));
            mLinkageCondition.setStateName(stateName);
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }
        updateView();
    }

    private void updateView() {
        ((RadioButton) whenRadioGroup.getChildAt(mLinkageCondition.getCompareTypeId() * 2 - 2)).setChecked(true);
        statusSeekBar.setValue((int) mLinkageCondition.getBeginValue());
        sectionSeekBar.setValue((int) mLinkageCondition.getBeginValue(),(int) mLinkageCondition.getEndValue());
        if(mLinkageCondition.getStateName().equals(LLDeviceUtil.LIGHT)){
            statusSeekBar.setMinMaxValue(0,100);
            sectionSeekBar.setMinMaxValue(0,100);
        }else if(mLinkageCondition.getStateName().equals(LLDeviceUtil.KELVIN)){
            statusSeekBar.setMinMaxValue(2700,7200);
            sectionSeekBar.setMinMaxValue(2700,7200);
        }else if(mLinkageCondition.getStateName().equals(LLDeviceUtil.STROKE_PERCENTAGE)){
            statusSeekBar.setMinMaxValue(0,100);
            sectionSeekBar.setMinMaxValue(0,100);
        }
        ((RadioButton) whenRadioGroup.getChildAt(mLinkageCondition.getCompareTypeId() * 2 - 2)).setChecked(true);
        statusSeekBar.setValue((int) mLinkageCondition.getBeginValue());
        statusSeekBar.setUnitValue(unit);
        sectionSeekBar.setValue((int) mLinkageCondition.getBeginValue(),(int) mLinkageCondition.getEndValue());
        sectionSeekBar.setUnit(unit);
        setActionType();
    }

    private void setActionType(){
        settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
        whenRadioGroup.setVisibility(mLinkageCondition.getActionTypeId() == 1 ? View.VISIBLE : View.GONE);
        statusSeekBar.setVisibility(mLinkageCondition.getActionTypeId() == 1 ? View.VISIBLE : View.GONE);
        sectionSeekBar.setVisibility(mLinkageCondition.getActionTypeId() == 1 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onRightClick(View view) {
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }
        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_action:
                showBottomListDialog();
                break;
        }
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            mBottomActionDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("变为");
            data.add("处于");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mBottomActionDialog.dismiss();
                    mLinkageCondition.setActionTypeId(position + 1);
                    if(mLinkageCondition.getStateName().equals(LLDeviceUtil.LIGHT)){
                        if(mLinkageCondition.getActionTypeId() == 1){
                            mLinkageCondition.setBeginValue(50);
                            mLinkageCondition.setEndValue(50);
                            statusSeekBar.setValue((int) mLinkageCondition.getBeginValue());
                        }else{
                            mLinkageCondition.setBeginValue(40);
                            mLinkageCondition.setEndValue(60);
                            sectionSeekBar.setValue((int) mLinkageCondition.getBeginValue(),(int) mLinkageCondition.getEndValue());
                        }
                    }else if(mLinkageCondition.getStateName().equals(LLDeviceUtil.KELVIN)){
                        if(mLinkageCondition.getActionTypeId() == 1){
                            mLinkageCondition.setBeginValue(3000);
                            mLinkageCondition.setEndValue(3000);
                            statusSeekBar.setValue((int) mLinkageCondition.getBeginValue());
                        }else{
                            mLinkageCondition.setBeginValue(2500);
                            mLinkageCondition.setEndValue(35000);
                            sectionSeekBar.setValue((int) mLinkageCondition.getBeginValue(),(int) mLinkageCondition.getEndValue());
                        }
                    }else if(mLinkageCondition.getStateName().equals(LLDeviceUtil.STROKE_PERCENTAGE)){
                        if(mLinkageCondition.getActionTypeId() == 1){
                            mLinkageCondition.setBeginValue(50);
                            mLinkageCondition.setEndValue(50);
                            statusSeekBar.setValue((int) mLinkageCondition.getBeginValue());
                        }else{
                            mLinkageCondition.setBeginValue(40);
                            mLinkageCondition.setEndValue(60);
                            sectionSeekBar.setValue((int) mLinkageCondition.getBeginValue(),(int) mLinkageCondition.getEndValue());
                        }
                    }
                    setActionType();
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }
}